<?php

namespace app\controllers;

use app\librarys\BaseController;
use app\models\Credit;
use app\models\Institution;
use app\models\Manager;
use app\models\Order;
use app\models\Permission;
use app\models\Recharge;

class DashboardController extends BaseController
{
    public function actionIndex()
    {
        $institutionCode = Manager::getCurrentManager()['institution_code'];

        $currentManager = Manager::getCurrentManager();
        $institutions = Institution::getAllEnabledListWithHierarchy($currentManager['institution_code']);

        $sellingReportItems = Order::getReportWithQuantityAndSellingPrice();

        $institutionItems = Institution::getListByDashboard($institutionCode);

        //获取最近付款日期、金额(充值)
        $last_recharg = Recharge::getLastRecharged($institutionCode);


        $isAdmin = Permission::isSystemAdmin();
        //获取订单数
        $all_order_list = Order::getOrderCount($institutionCode, $isAdmin);
        //获取订单数(已完成)
        $complete_order_list = Order::getOrderNumberCount($institutionCode, $isAdmin);

        //获取订单金额
        $order_price_list = Order::getOrderPriceCount($institutionCode, $isAdmin);
        //获取授信
        $credit = null;
        if (!$isAdmin) {
            $credit = Credit::getCreditByInstitutionCode($institutionCode);
        } else {
            $credit = Credit::getCreditSum();
        }

        return $this->render('index',
            [
                'institution' => $institutions,
                'selling_report_items' => $sellingReportItems,
                'institution_items' => $institutionItems,

                'last_recharg' => $last_recharg,
                'all_order_list' => $all_order_list,
                'complete_order_list' => $complete_order_list,
                'order_price_list' => $order_price_list,
                'credit' => $credit
            ]);
    }

    /**
     * 获取销售统计数据
     * @return string
     */
    public function actionGetSellAmountHandler()
    {
        $institution_code = Manager::getCurrentManager()['institution_code'];
        $isAdmin = Permission::isSystemAdmin();

        $type = \Yii::$app->request->post('type');
        $result = null;
        switch ($type) {
            case 'month':
                $result = Order::getReportWithQuantityAndSellingPriceByMonth($institution_code, $isAdmin);
                break;
            case 'quarter':
                $result = Order::getReportWithQuantityAndSellingPriceByQuarter($institution_code, $isAdmin);
                break;
            case 'year':
                $result = Order::getReportWithQuantityAndSellingPriceByYear($institution_code, $isAdmin);
                break;
        }


        return $this->renderJson($result, 0, '');
    }

    /**
     * 获取支付统计数据
     * @return string
     */
    public function actionGetRechargedAmountHandler()
    {
        $institution_code = Manager::getCurrentManager()['institution_code'];
        $isAdmin = Permission::isSystemAdmin();

        $type = \Yii::$app->request->post('type');
        $result = null;
        switch ($type) {
            case 'month':
                $result = Recharge::getRechargedAmountByMonth($institution_code, $isAdmin);
                break;
            case 'quarter':
                $result = Recharge::getRechargedAmountByQuarter($institution_code, $isAdmin);
                break;
            case 'year':
                $result = Recharge::getRechargedAmountByYear($institution_code, $isAdmin);
                break;
        }


        return $this->renderJson($result, 0, '');
    }
}